<template>
	<view class="index">
		<view class="topHeader"></view>
		<view class="listOut">
			<view class="list" v-for="(item, index) in list" :key="index" @tap="tipPage(item.path, index)"
			:style="{'backgroundColor': item.bgColor}">
				<image :src="item.img" mode="" class="imgs"></image>
				<view v-if="index !== 3">
					<view class="name" >{{item.name}}</view>
					<view class="tips" v-if="index !== 3">{{item.tips}}</view>
					<view class="tips tipsReal" v-if="index == 3 && userInfo.name" style="align-items: flex-start;">
						<text style="color: #2DBDA2;margin-right: 20rpx;">{{userInfo.name}}</text>
						
						<text style="padding-right: 20rpx;">{{userInfo.deptName}}</text>
					</view>
					<view class="tips tipsReal" v-if="index == 3 && !userInfo.name">
						
							{{item.tips}}
							<view class="tipsBox">
								前往认证
								<view class="tipIn"></view>
							</view>

					</view>
				</view>
				<view v-else>
					<view class="name" v-if="userInfo.name">{{userInfo.name}}</view>
					<view class="name" v-else>{{item.name}}</view>
					<view class="tips tipsReal" v-if="userInfo.name" style="align-items: flex-start;">
						<!-- <text style="color: #2DBDA2;margin-right: 20rpx;">{{userInfo.name}}</text> -->
						
						<text style="padding-right: 20rpx;">{{userInfo.deptName}}</text>
					</view>
					<view class="tips tipsReal" v-if="index == 3 && !userInfo.name">
						
							{{item.tips}}
							<view class="tipsBox">
								前往认证
								<view class="tipIn"></view>
							</view>
					
					</view>
				</view>
				
			</view>
			<!-- {{unionid}} -->
			<!-- <view style="word-wrap: break-word;">{{str}}</view> -->
			<view class="safeTip">燃气安全云防护平台  让用户安全放心用气</view>
		</view>
		
	</view>
</template>

<script>
	// const uniID = require('uni-id')
	import {checkUserInfo} from './js/api.js'
	export default {
		data() {
			return {
				list: [
					{
						img: require('../../static/index/ercodes.png'),
						name: '设备查询',
						tips: '显示物联设备信息',
						path: '../search/search',
						bgColor: '#EFF4FF'
					},
					{
						img: require('../../static/index/listIcon.png'),
						name: '设备信息',
						tips: '显示物联设备信息',
						path: '../list/equList',
						bgColor: '#EAF7FF'
					},
					{
						img: require('../../static/index/yujing.png'),
						name: '故障列表',
						tips: '定位安装物联设备',
						path: '../list/list',
						bgColor: '#FFFBF5'
					},
					{
						img: require('../../static/index/real.png'),
						name: '实名认证',
						tips: '您还未实名认证',
						path: '../user/userInfo',
						bgColor: '#E9F7F5'
					}
				],
				userInfo: {},
				unionid: uni.getStorageSync('unionid'),
				str: ''
			}
		},
		onLoad() {
			// uni.navigateTo({
			// 	url: '../user/userInfo'
			// })
			// uni.request({
			// 	url: 'http://112.126.84.165:8080/v3/api-docs/api/app/auth'
			// })
		},
		onShow() {
			if (uni.getStorageSync('info')) {
				this.userInfo = uni.getStorageSync('info')
			}
		},
		methods: {
			toast (title) {
				uni.showToast({
					title: title,
					icon: 'none',
					duration: 2000
				})
			},
			tipPage(path,index) {
				if (index === 1 || index === 0 || index === 2){
					console.log(path)
					if (uni.getStorageSync('unionid')) {
						let unionid = uni.getStorageSync('unionid')
						let params = {
							unionid: unionid
						}
						checkUserInfo(params).then((res) => {
							console.log(res.data)
							if (res.data.status === 0) {
								if (uni.getStorageSync('info')) {
									uni.removeStorageSync('info')
								}
								uni.navigateTo({
									url: '../user/userInfo'
								})
							} else {
								uni.setStorageSync('info', res.data)
								uni.navigateTo({
									url: `${path}`
								})
							}
						})
					} else {
						uni.showLoading({
							title: '加载中···',
							mask: true
						})
						wx.login({
						    success: (res) => {
								console.log(res.code)
								if (res.code) {         //微信登录成功 已拿到code  
						            uni.request({  
						                url: 'https://www.krtwl.com/api/app/getUnionid?code=' + res.code,  
						                method:'GET',
						                success: (cts) => {
											console.log(cts.data.unionid)
											this.str = JSON.stringify(cts.data)
											uni.setStorageSync('unionid', cts.data.unionid)
											let params = {
												unionid: cts.data.unionid
											}
											checkUserInfo(params).then((res) => {
												console.log(res.data)
												if (res.data.status === 0) {
													if (uni.getStorageSync('info')) {
														uni.removeStorageSync('info')
													}
													this.toast('请先进行实名认证')
												} else {
													uni.setStorageSync('info', res.data)
													uni.navigateTo({
														url: `${path}`
													})
												}
											})
										},
						    		})
								}
						    }	
						})
					}            
				} else {        
					console.log('----------')
					console.log(uni.getStorageSync('unionid'))
					console.log('----------')
					if (!uni.getStorageSync('unionid')) {
						console.log('进入')
						wx.login({
						    	success: (res) => {
									console.log(res.code)
									if (res.code) {         //微信登录成功 已拿到code  
						    	        uni.request({  
						    	            url: 'https://www.krtwl.com/api/app/getUnionid?code=' + res.code,  
						    	            method:'GET',
						    	            success: (cts) => {
												this.str = JSON.stringify(cts.data)
												console.log(cts.data.unionid)
												if (cts.data.unionid) {
													let unionid = cts.data.unionid
													uni.setStorageSync('unionid', unionid)
													let params = {
													unionid: unionid
												}
												checkUserInfo(params).then((res) => {
													console.log(res.data)
													if (res.data.status === 0) {
														if (uni.getStorageSync('info')) {
															uni.removeStorageSync('info')
														}
														uni.navigateTo({
															url: '../user/userInfo'
														})
													} else {
														uni.setStorageSync('info', res.data)
														// this.userInfo = res.data
														uni.navigateTo({
															url: '../user/realInfo'
														})
													}
												})
											}
						                }
									});
								}
							},
							fail:(err) => {
								console.log(err)
							}
						})
					} else {
						let unionid = uni.getStorageSync('unionid')
						let params = {
							unionid: unionid
						}
						checkUserInfo(params).then((res) => {
							console.log(res.data)
							if (res.data.status === 0) {
								if (uni.getStorageSync('info')) {
									uni.removeStorageSync('info')
								}
								uni.navigateTo({
									url: '../user/userInfo'
								})
							} else {
								uni.navigateTo({
									url: '../user/realInfo'
								})
							}
						})
					}
				}
			},
			
		}
	}
</script>

<style lang="scss">
	.index{
		width: 100%;
		background: linear-gradient(0deg, #17A488, #2FBEA1, #7ADFCB);
		min-height: calc(100vh - 20rpx);
		padding-bottom: 20rpx;
		overflow: hidden;
		.topHeader{
			width: 100%;
			height: 400rpx;
			background-image: url();
			background-repeat: no-repeat;
			
			background-size: 100% 100%;
		}
		.listOut{
			width: 690rpx;
			height: 1150rpx;
			background: #FFFFFF;
			box-shadow: 0px 4rpx 20rpx 0px rgba(102, 102, 102, 0.3);
			border-radius: 15rpx;
			margin: 0 auto;
			padding-top: 10rpx;
			margin-top: 20rpx;
			overflow: hidden;
			position: relative;
		}
		.list{
			margin: 40rpx auto 0;
			width: 630rpx;
			height: 200rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 4rpx 20rpx 0rpx rgba(102, 102, 102, 0.3);
			border-radius: 15rpx;
			display: flex;
			justify-content: flex-start;
			align-items: center;
			flex-direction: row;
			.imgs{
				width: 110rpx;
				flex: 0 0 110rpx;
				height: 110rpx;
				margin-left: 100rpx;
				margin-right: 20rpx;
			}
			.name{
				color: #333333;
				font-size: 32rpx;
				font-weight: 500;
			}
			.tips{
				color: #999999;
				font-size: 26rpx;
				margin-top: 20rpx;
			}
			.tipsReal{
				width: 100%;
				display: flex;
				justify-content: center;
				align-items: center;
				.tipsBox{
					color: #2DBDA2;
					font-size: 24rpx;
					position: relative;
					padding: 0 8rpx;
					margin-left: 20rpx;
				}
				.tipsBox::before{
					content: '';
					position: absolute;
					top: 0;
					left: 0;
					width: 7rpx;
					height: 10rpx;
					border-top: 2rpx solid #999;
					border-left: 2rpx solid #999;
				}
				.tipsBox::after{
					content: '';
					position: absolute;
					bottom: 0;
					right: 0;
					width: 7rpx;
					height: 10rpx;
					border-bottom: 2rpx solid #999;
					border-right: 2rpx solid #999;
				}
			}
		}

		.safeTip{
			color: #333333;
			font-size: 28rpx;
			text-align: center;
			position: absolute;
			bottom: 30rpx;
			width: 100%;
		}
	}
</style>
